/**
 * Collection List
 *  -  system_BannerDefinitions
 *  -  system_messages
 *  -  system_userFeedbacks
 *  -  system_appStartInfo
 *  -  system_deviceInfo
 *  -  system_codeShortMessage
 *  -  system_loginOrRegisterLog
 *  -  system_appHomeModules
 *  -  system_specialTopics
 *  -  system_freshNews
 *  -  system_pushRecords
 *  -  system_operateMessages
 */

var Schema = mongoose.Schema;

/**
 *======================Banner Definition =============================
 */
var bannerMongoSchema = new Schema({
    stickyPage: {type: String, enum: ['questionHomePage'], default: 'questionHomePage'},  //定义这个banner放在哪个页面，可扩充，
    community: {type: Schema.Types.ObjectId, ref: 'community'},
    banner: [{
        displayOrder: Number,
        desc: String,
        activityId: String,
        title: String,
        backgroundImageURL: String
    }],
    isValid: {type: Boolean, default: true},
    lastModifiedBy: {type: Schema.Types.ObjectId, ref: 'user'},
    dateLastModified: {type: Date, default: Date.now},
    createdBy: {type: Schema.Types.ObjectId, ref: 'user'},
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('system_BannerDefinitions', bannerMongoSchema, 'system_bannerdefinitions');


/**
 * =================== Message ====================================
 */
var messageMongoSchema = new Schema({
    category: {
        type: String,
        enum: ['system', 'activity', 'friend', 'question', 'partake', 'neighbourActivity', 'topic']
    }, //消息类别；
    content: {
        contentId: {type: Schema.Types.ObjectId, ref: 'question'},
        title: String,
        questionDesc: String,                                //问题描述
        replyDesc: String,                                   //回复描述
        replyId: String,
        contentType: {
            type: String,
            enum: ['myQuestion', 'myFollowup', 'myReply', 'myThank', 'myAdopt', 'myFollowupUser', 'partake', 'reply2reply', 'replyAt', 'activity']
        },  //内容类型
        questionCreatedBy: {type: Schema.Types.ObjectId, ref: 'user'}  //问题创建者
    },
    images: [{imageURL: String}],                          //消息图片
    fromUser: {type: Schema.Types.ObjectId, ref: 'user'},//从谁来
    toUser: {type: Schema.Types.ObjectId, ref: 'user'},  //发给谁
    readFlag: {type: Boolean, default: false},               //用户是否已经阅读
    community: {type: Schema.Types.ObjectId, ref: 'community'},
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('system_messages', messageMongoSchema, 'system_messages');


//================================反馈信息================================
var userFeedbackSchema = new Schema({
    desc: String,
    dateCreated: {type: Date, default: Date.now},
    createdBy: {type: Schema.Types.ObjectId, ref: 'user'},
    status: {type: Boolean, default: false},
    community: {type: Schema.Types.ObjectId, ref: 'community'}
});

mongoose.model('system_userFeedbacks', userFeedbackSchema, 'system_userfeedbacks');

//================================App启动信息=============================
var appStartInfoSchema = new Schema({
    deviceId: String,
    net_provider: String,
    net_type: String,
    gps_x: Number,
    gps_y: Number,
    gps_name: String,
    userId: {type: Schema.Types.ObjectId, ref: 'user'},
    community: {type: Schema.Types.ObjectId, ref: 'community'},                //所属小区
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('system_appStartInfo', appStartInfoSchema, 'system_appstartinfos');


//================================设备信息===============================
var deviceInfoSchema = new Schema({
    deviceId: String,
    mobile_model: String,
    osv: String,
    width: Number,
    height: Number,
    sim_no: String,
    mac: String,
    channelId: String,
    userId: {type: Schema.Types.ObjectId, ref: 'user'},
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('system_deviceInfo', deviceInfoSchema, 'system_deviceinfos');


//================================验证码短信记录===============================
var verificationCodeShortMessage = new Schema({
    deviceId: String,
    mobilePhone: String,
    verificationCode: Number,
    messageId: String,
    errCode: String,
    status: String,
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('system_codeShortMessage', verificationCodeShortMessage, 'system_codeshortmessages');

//================================================================================
var loginOrRegisterLog = new Schema({
    deviceId: String,
    mobilePhone: String,
    password: String,
    message: String,
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('system_loginOrRegisterLog', loginOrRegisterLog, 'system_loginorregisterlogs');


//================================================================================
var appHomeModules = new Schema({
    title: String,
    imageURL: String,
    fieldName: String,
    displayOrder: Number,
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('system_appHomeModules', appHomeModules, 'system_apphomemodules');

//====================================专题===========================================
var specialTopicSchema = new Schema({
    title: String,
    community: [
        {type: Schema.Types.ObjectId, ref: 'community'}
    ],
    imageURL: String,
    video: Array,
    desc: String,
    isValid: {type: Boolean, default: false},
    dateCreated: {type: Date, default: Date.now},
    comments: [{
        commentDesc: String,
        commentedBy: {type: Schema.Types.ObjectId, ref: 'user'},     //评论人
        commentedDate: {type: Date, default: Date.now}                  //评论日期
    }],
    shareDesc: String,
    shareIcon: String,
    summary: String,
    url: String,
    dateLastModified: {type: Date, default: Date.now}
});
mongoose.model('system_specialTopics', specialTopicSchema, 'system_specialTopics');

var shareContent = new Schema({
    key:String,              //关键字 标识专题、活动、或者其他h5页面的关键字
    title: String,
    desc: String,
    image: String,
    url: String,
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('mobile_app_share_content', shareContent, 'mobile_app_share_content');



//=================================新鲜事儿===========================================
var freshNewsSchema = new Schema({
    openType: Number,
    tag: String,
    title: String,
    url: String,
    displayOrder: {type: Number, default: 0},
    newsId: String,
    bgColor: String,
    createdBy: {type: Schema.Types.ObjectId, ref: 'user'},
    community: [
        {type: Schema.Types.ObjectId, ref: 'community'}
    ],
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('system_freshNews', freshNewsSchema, 'system_freshNews');


//=======================================================================================
var pushRecordSchema = new Schema({
    messageId: String,
    message: String,
    tag: String,
    fromIp: String,
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('system_pushRecords', pushRecordSchema, 'system_pushRecords');


//=======================================================================================
var operateMessageSchema = new Schema({
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    community: {type: Schema.Types.ObjectId, ref: 'community'},
    message: String,
    messageId: String,
    createdBy: {type: Schema.Types.ObjectId, ref: 'user'},
    pushLogId: String,
    tag: String,
    url: String,
    openType: Number,
    clickSource: String,
    readFlag: {type: Boolean, default: false},               //用户是否已经阅读
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('system_operateMessages', operateMessageSchema, 'system_operateMessages');


var operateMessageLogSchema = new Schema({
    _class:String,
    contentId: String,
    title: String,
    openType: Number,
    url: String,
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('system_operateMessages_log', operateMessageLogSchema, 'system_operateMessages_log');

var chatMessageSchema = new Schema({
    from: String,                            //发送人username
    to: String,                              //接收人的username或者接收group的id
    messageId: String,                       //消息id
    chatType: String,                        //用来判断单聊还是群聊。chat:单聊，groupchat:群聊
    message: String,                         //消息内容
    messageTime: Date,                       //消息发送时间
    messageType: String,                     //消息类型。txt:文本消息, img:图片, loc：位置, audio：语音
    url: String,                             //图片语音等文件的网络url，图片和语音消息有这个属性
    filename: String,                        //文件名字，图片和语音消息有这个属性
    secret: String,                          //获取文件的secret，图片和语音消息有这个属性
    length: Number,                          //语音时长，单位为秒，这个属性只有语音消息有
    lat: String,                             //发送的位置的纬度，只有位置消息有这个属性
    lng: String,                             //位置经度，只有位置消息有这个属性
    address: String,                         //位置消息详细地址，只有位置消息有这个属性
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('system_chatMessages', chatMessageSchema, 'system_chatMessages');

var ugcIndexSchema = new Schema({
    ugcId: String,
    category: String,
    displayOrder: Number,
    tag: [
        {type: Schema.Types.ObjectId, ref: 'masterData_Dictionaries'}
    ],
    community: {type: Schema.Types.ObjectId, ref: 'community'},
    createdBy: {type: Schema.Types.ObjectId, ref: 'user'},
    level: {type: Number, default: 0},                                                  //0，普通帖 1，精华帖
    dateCreated: {type: Date, default: Date.now},
    lastReply: {type: Date, default: Date.now}
});
mongoose.model('system_ugcIndex', ugcIndexSchema, 'system_ugcIndex');

var ugcReplySchema = new Schema({
    ugcId: String,
    replyId: String,
    category: String,
    community: {type: Schema.Types.ObjectId, ref: 'community'},
    createdBy: {type: Schema.Types.ObjectId, ref: 'user'},
    repliedBy: {type: Schema.Types.ObjectId, ref: 'user'},
    aroundCommunity: [                                                      //周边小区
        {type: Schema.Types.ObjectId, ref: 'community', index: true}
    ],
    ugcReply: [{
        replyId: String,
        dateReplied: {type: Date, default: Date.now}
    }],
    dateCreated: {type: Date, default: Date.now},
    lastReply: {type: Date, default: Date.now}
});
mongoose.model('system_ugcReply', ugcReplySchema, 'system_ugcReply');

var promotionCodeSchema = new Schema({
    code: String,
    dateCreated: {type: Date, default: Date.now},
    expirationDate: Date,
    owner: {type: String, default: ''},
    isValid: {type: Boolean, default: true}
});
mongoose.model('system_promotionCode', promotionCodeSchema, 'system_promotionCode');

var offlineChatMessageSchema = new Schema({
    callId: String,
    eventType: String,
    timestamp: Number,
    from: String,
    to: String,
    readFlag: {type: Boolean, default: false},
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('system_offlineChatMessage', offlineChatMessageSchema, 'system_offlineChatMessage');

var appErrorLog = new Schema({
    userId: {type: Schema.Types.ObjectId, ref: 'user'},
    deviceId: String,
    osType: String,
    osModel: String,
    appvName: String,
    sysvName: String,
    errorCode: Number,
    url: String,
    errorMsg: String,
    dateCreated: {type: Date, default: Date.now},
    dateLastModified: {type: Date, default: Date.now}
});
mongoose.model('app_error_log', appErrorLog, 'app_error_log');

var splashSchema = new Schema({
    startDate: Date,
    endDate: Date,
    desc: String,
    duration: Number,
    image: String,
    isValid: {type: Boolean, default: true},
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('system_splashScreen', splashSchema, 'system_splashScreen');

/* 宝宝树66小区二次改造    add   gct  20170811   start   */
var splReasonCancelSchema = new Schema({

    msgSort: Number,
    message: String,
    isValid: {type: Boolean, default: true},
    dateCreated: {type: Date, default: Date.now},
    /* 宝宝树66小区 sprint5 创始人申请页面 拒绝用户小区大使申请修改 modify wangcan 20171206 start */
    msgType: Number // 0: 取消大使 2：拒绝创始人申请
    /* 宝宝树66小区 sprint5 创始人申请页面 拒绝用户小区大使申请修改 modify wangcan 20171206 end */
});
mongoose.model('system_cancel_reason_message', splReasonCancelSchema, 'system_cancel_reason_message');
/*  宝宝树66小区二次改造    add   gct  20170811   end   */